package com.dataconver.config;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;

/**
 * 数据同步配置类
 */
@Configuration
public class DataSyncConfiguration {
    
    @Autowired
    private DatabaseProperties databaseProperties;
    
    /**
     * SQLServer数据源
     */
    @Bean(name = "sqlserverDataSource")
    public DataSource sqlserverDataSource() {
        HikariConfig config = new HikariConfig();
        config.setJdbcUrl(databaseProperties.getSqlserver().getUrl());
        config.setUsername(databaseProperties.getSqlserver().getUsername());
        config.setPassword(databaseProperties.getSqlserver().getPassword());
        config.setDriverClassName(databaseProperties.getSqlserver().getDriver());
        config.setMaximumPoolSize(databaseProperties.getSqlserver().getMaxPoolSize());
        config.setMinimumIdle(databaseProperties.getSqlserver().getMinIdle());
        config.setPoolName("SQLServer-HikariCP");
        
        // SQLServer不需要设置时区属性
        
        return new HikariDataSource(config);
    }
    
    /**
     * MySQL数据源
     */
    @Bean(name = "mysqlDataSource")
    @Primary
    public DataSource mysqlDataSource() {
        HikariConfig config = new HikariConfig();
        config.setJdbcUrl(databaseProperties.getMysql().getUrl());
        config.setUsername(databaseProperties.getMysql().getUsername());
        config.setPassword(databaseProperties.getMysql().getPassword());
        config.setDriverClassName(databaseProperties.getMysql().getDriver());
        config.setMaximumPoolSize(databaseProperties.getMysql().getMaxPoolSize());
        config.setMinimumIdle(databaseProperties.getMysql().getMinIdle());
        config.setPoolName("MySQL-HikariCP");
        
        // MySQL时区已在URL中设置
        
        return new HikariDataSource(config);
    }
}
